﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 物料信息
    /// </summary>
    [ApiDescriptionSettings("基础信息", Name = "WmsMaterial", Order = 100)]
    [Route("api/[Controller]")]
    public class WmsMaterialService : IWmsMaterialService, IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsMaterial,MasterDbContextLocator> _wmsMaterialRep;


        public WmsMaterialService(
            IRepository<WmsMaterial,MasterDbContextLocator> wmsMaterialRep
        )
        {
            _wmsMaterialRep = wmsMaterialRep;
        }

        /// <summary>
        /// 分页查询物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<WmsMaterialOutput>> Page([FromQuery] WmsMaterialSearch input)
        {
            var wmsMaterials = await _wmsMaterialRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Materialname), u => EF.Functions.Like(u.MaterialName, $"%{input.Materialname.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialno), u => EF.Functions.Like(u.MaterialNo, $"%{input.Materialno.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialbatch), u => EF.Functions.Like(u.MaterialBatch, $"%{input.Materialbatch.Trim()}%"))
                                     .Where(input.Inspectionmethod != null, u => u.InspectionMethod == input.Inspectionmethod)
                                     .Where(input.Materialtype != null, u => u.MaterialType == input.Materialtype)
                                     .Where(!string.IsNullOrEmpty(input.Materialspec), u => u.MaterialSpec == input.Materialspec)
                                     .Where(input.Unittype != null, u => u.UnitType == input.Unittype)
                                     .Where(input.Unitno != null, u => u.UnitNo == input.Unitno)
                                     .Where(input.Keymaterials != null, u => u.Keymaterials == input.Keymaterials)
                                     .OrderBy(PageInputOrder.OrderBuilder<WmsMaterialSearch>(input))
                                     .ProjectToType<WmsMaterialOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return wmsMaterials;
        }

        /// <summary>
        /// 增加物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task Add(AddWmsMaterialInput input)
        {
            var isExit = await _wmsMaterialRep.AnyAsync(n => n.MaterialName == input.Materialname && n.MaterialNo==input.Materialno);
            if (isExit) throw Oops.Oh("存在的相同的物料名称和批次！");
            var wmsMaterial = input.Adapt<WmsMaterial>();
            await _wmsMaterialRep.InsertAsync(wmsMaterial);
        }

        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task Delete(DeleteWmsMaterialInput input)
        {
            var wmsMaterial = await _wmsMaterialRep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _wmsMaterialRep.DeleteAsync(wmsMaterial);
        }

        /// <summary>
        /// 更新物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task Update(UpdateWmsMaterialInput input)
        {
            var isExist = await _wmsMaterialRep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var wmsMaterial = input.Adapt<WmsMaterial>();
            await _wmsMaterialRep.UpdateAsync(wmsMaterial,ignoreNullValues:true);
        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<WmsMaterialOutput> Get([FromQuery] QueryeWmsMaterialInput input)
        {
            return (await _wmsMaterialRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<WmsMaterialOutput>();
        }

        /// <summary>
        /// 获取物料信息列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        public async Task<List<WmsMaterialOutput>> List([FromQuery] WmsMaterialInput input)
        {
            return await _wmsMaterialRep.DetachedEntities.ProjectToType<WmsMaterialOutput>().ToListAsync();
        }    

    }
}
